<?php
// +----------------------------------------------------------------------
// | FRANSCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013  All rights reserved.
// +----------------------------------------------------------------------
// | Author:
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\common\controller\Admin;

/**
 * @title 操作日志
 * @description 行为管理
 */
class Action extends Admin
{

    /**
     * @title 操作日志列表
     * @author huajie <banhuajie@163.com>
     */
    public function log()
    {

        //获取列表数据
        $list = model('ActionLog')->where([])->order('id DESC')->paginate(10);

        $data = array(
            'list' => $list,
            'page' => $list->render(),
        );
        $this->assign($data);
        $this->setMeta('操作日志');
        return $this->fetch();
    }

    /**
     * @title 查看操作日志
     * @author huajie <banhuajie@163.com>
     */
    public function detail($id = 0)
    {
        $model = model('ActionLog');
        if (empty($id)) {
            return $this->error('参数错误！');
        }

        $info = $model::get($id);

        $info['title'] = get_action($info['action_id'], 'title');
        $info['user_id'] = get_username($info['user_id']);
        $info['action_ip'] = long2ip($info['action_ip']);
        $info['create_time'] = date('Y-m-d H:i:s', $info['create_time']);
        $data = array(
            'info' => $info,
            'keyList' => $model->keyList,
        );
        $this->assign($data);
        $this->setMeta('查看行为日志');
        return $this->fetch();
    }

    /**
     * @title 删除操作日志
     * @param mixed $id
     * @author huajie <banhuajie@163.com>
     */
    /*public function dellog()
    {
        $id = $this->getArrayParam('id');
        if (empty($id)) {
            return $this->error("非法操作！", '');
        }
        $map['id'] = array('IN', $id);
        $res = db('ActionLog')->where($map)->delete();
        if ($res !== false) {
            action_log('delete_actionlog', 'ActionLog', $id, session('user_auth.uid'));
            return $this->success('删除成功！');
        } else {
            return $this->error('删除失败！');
        }
    }*/

    /**
     * @title 清空日志
     */
    public function clear($id = '')
    {
        $res = db('ActionLog')->where('1=1')->delete();
        if ($res !== false) {
            //记录行为
            action_log('clear_actionlog', 'ActionLog', $id, session('user_auth.uid'));
            return $this->success('日志清空成功！');
        } else {
            return $this->error('日志清空失败！');
        }
    }
}